﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Web.CrmContacto.Code;
using Web.Framework;
using CrmContacto.WebApp.Code.InnerClasses;
using CrmContacto.Contracts.Contracts.Misc;
using Web.Framework.Helpers;

namespace CrmContacto.WebApp.App
{
    public partial class CustomerImport : BasePage
    {
        public string Pais
        {
            get
            {
                return ConfigurationManager.AppSettings["LenguajePorDefecto"];
            }
        }

        //private List<LinkButton> actionButtons = new List<LinkButton>();
        //protected override void OnInit(EventArgs e)
        //{
        //    base.OnInit(e);

        //    LinkButton nuevaCategoriaButton = new LinkButton();
        //    nuevaCategoriaButton.ID = "BoCategoriasActionMenuNuevaCategoria";
        //    nuevaCategoriaButton.Text = LocalizationProvider.Instance["Bo.Categorias.ActionMenu.NuevaCategoria"];
        //    nuevaCategoriaButton.Click += new EventHandler(nuevaCategoriaButton_Click);

        //    actionButtons.Add(nuevaCategoriaButton);
        //    base.LoadContextActions(actionButtons);

        //}

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                lnkVerResultados.Attributes.Add("aspnetid", lnkVerResultados.ClientID);

                if (!IsPostBack)
                {
                    LoadCustomers();
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        private void LoadCustomers()
        {
            List<ClienteWrap> clienteList = HttpContext.Current.Cache.Get(HttpContext.Current.Session.SessionID + MiscClass.CustomerImportRecordsKey) as List<ClienteWrap>;
            if (clienteList != null)
            {
                this.Grid.DataSource = clienteList;
                this.Grid.DataBind();

                pnlCustomers.Visible = true;           
            }
        }

        protected override void ApplySecurity()
        {
            //this.btnAgregarNuevo.Visible = HasPermissionFor(PlanProdSecurity.DESTINO_CREAR);

            //if (this.GridCategorias.DataSource != null)
            //{
            //    this.GridCategorias.Columns[0].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_EDITAR);
            //    this.GridCategorias.Columns[1].Visible = HasPermissionFor(PlanProdSecurity.DESTINO_ELIMINAR);
            //}
        }        

        protected void Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Cells[0].Attributes.Add("title", "Editar");
                    ClienteWrap clienteWrap = e.Row.DataItem as ClienteWrap;
                    if (!clienteWrap.FueIncorporado)
                    {
                        e.Row.Cells[0].Text = string.Empty;
                    }
                    //e.Row.Cells[1].Attributes.Add("title", "Eliminar")
                    //ImageButton btnEliminarCtrl = (ImageButton)e.Row.FindControl("btnEliminar");
                    //btnEliminarCtrl.OnClientClick = string.Format("return confirm('" + LocalizationProvider.Instance["Bo.Messages.ConfirmarEliminarRegistro"] + "')", ((MaestroBase)e.Row.DataItem).Nombre);
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void Grid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (e.CommandName == "editar")
                {
                    int contactoId = Convert.ToInt32(this.Grid.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
                    this.Response.Redirect(string.Format(Request.Url.Scheme + "://" + Request.Url.Authority + MiscClass.UrlCustomerEdit + "?Id={0}", contactoId), true);
                }              
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkVerResultados_Click(object sender, EventArgs e)
        {
            try
            {
                LoadCustomers();
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }

        protected void lnkExportarAExcel_Click(object sender, EventArgs e)
        {
            try
            {
                bool previousAllowPaging = this.Grid.AllowPaging;
                this.Grid.AllowPaging = false;

                LoadCustomers();

                GridViewExportExcelUtil.Export("Categorias.xls", this.Grid, 0);

                this.Grid.AllowPaging = previousAllowPaging;
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}